package tianrun.ziguan.api.calculate.config.service.rollback.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tianrun.ziguan.api.calculate.config.dto.request.RollbackByAssetParam;
import tianrun.ziguan.api.calculate.config.dto.request.RollbackDataParam;
import tianrun.ziguan.api.calculate.config.entity.TAssetGroup;
import tianrun.ziguan.api.calculate.config.entity.TCalAsset;
import tianrun.ziguan.api.calculate.config.service.asset.group.ITAssetGroupService;
import tianrun.ziguan.api.calculate.config.service.cal.asset.ITCalAssetService;
import tianrun.ziguan.api.calculate.config.service.redis.RedisMessagePublisher;
import tianrun.ziguan.api.calculate.config.service.rollback.IRollbackService;
import tianrun.ziguan.api.common.util.ListUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class RollbackServiceImpl implements IRollbackService {
    private final RedisMessagePublisher redisMessagePublisher;
    private final ITCalAssetService calAssetService;
    private final ITAssetGroupService assetGroupService;
    @Value("${spring.redis.rollBackChannelName}")
    private String channelName;
    public RollbackServiceImpl(RedisMessagePublisher redisMessagePublisher, ITCalAssetService calAssetService, ITAssetGroupService assetGroupService) {
        this.redisMessagePublisher = redisMessagePublisher;
        this.calAssetService = calAssetService;
        this.assetGroupService = assetGroupService;
    }

    @Override
    public boolean rollbackByAsset(RollbackByAssetParam param) {
        Assert.isTrue(!ListUtil.isEmpty(param.getAssetNum()),"请传入资产数据");
        //补全场信息
        Set<String> assetNums = param.getAssetNum();
        List<TCalAsset> calAssets = calAssetService.list(Wrappers.<TCalAsset>lambdaQuery().in(TCalAsset::getAssetNum, assetNums));
        Assert.isTrue(!calAssets.isEmpty(),"没有找到该资产");
        List<TAssetGroup> assetGroups = assetGroupService.list(Wrappers.<TAssetGroup>lambdaQuery().in(TAssetGroup::getId, calAssets.stream().map(TCalAsset::getAssetGroupId).collect(Collectors.toSet())));
        Map<Long, String> map = assetGroups.stream().collect(Collectors.toMap(TAssetGroup::getId, TAssetGroup::getName));
        Map<String, String> map1 = calAssets.stream().collect(Collectors.toMap(TCalAsset::getAssetNum, asset -> map.get(asset.getAssetGroupId()),(a,b)->b));
        ArrayList<RollbackDataParam> list = Lists.newArrayList();
        for (String assetNum : assetNums) {
            RollbackDataParam rollbackDataParam = new RollbackDataParam(param.getStartTime(),map1.get(assetNum),assetNum,param.getCalLabel(),param.getDrivingType(),param.getRollbackType());
            list.add(rollbackDataParam);
        }
        System.out.println("回退 "+JSON.toJSONString(list));
        redisMessagePublisher.publish(channelName, JSON.toJSONString(list));
        return true;
    }
}
